import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
import skimage.io as io
from skimage import util
from glob import glob
import trackpy
import ast
from ipywidgets import interact, widgets
# import code from the src
### import code from src folder
import sys
sys.path.append('../src/')
import chow_test_v
import area_analysis
import warnings
warnings.filterwarnings('ignore')
from sklearn.metrics import r2_score, median_absolute_error, mean_absolute_error
from sklearn.metrics import median_absolute_error, mean_squared_error, mean_squared_log_error
file_list=glob("../data/*_Dark_prepared.tif")
images_dict={}
for file_name in file_list:
images_prepared=io.imread(file_name)
file_prefix=os.path.split(file_name)[1].split('.')[0].split("_prepared")[0]
binary_file=file_name.split(".tif")[0]+'_segmented'+'.tif'
images_binary=io.imread(binary_file)
tracks=pd.read_csv("../data/tracks_overlay_id_"+file_prefix+'.csv')
tracks_big=pd.read_csv("../data/tracks_overlay_id_"+file_prefix+'_big.csv')
img_dict={"img":images_prepared,"binary":images_binary,"tracks":tracks,"tracks_big":tracks_big}
images_dict[file_prefix]=img_dict
Area_replicas=pd.DataFrame()
for file_prefix in images_dict.keys():
tracks=images_dict[file_prefix]["tracks_big"]
images=images_dict[file_prefix]["img"]
binary=images_dict[file_prefix]["binary"]
# set up grid for plt plot
sns.set(font_scale=2)
Ngroups=tracks.groupby('particle').ngroups
Nrows=np.int(np.ceil(Ngroups/5))
fig,ax=plt.subplots(nrows=Nrows,ncols=5,figsize=(40,Nrows*7.5))
axx=ax.ravel()
#analyse each trajectory separately
# 1) anomaly detection
# 2) chow test anomalies
# 3) fit according anomalies detection
counter=0
Area_file_prefix=pd.DataFrame()
for id, traj in tracks.groupby('particle'):
df_result=area_analysis.area_analysis(traj,axx[counter])
Area_file_prefix=pd.concat([Area_file_prefix,df_result])
if df_result.shape[0]==2:
handles, labels = axx[counter].get_legend_handles_labels()
counter=counter+1
fig.suptitle(file_prefix,x=0.5, y=0.80)
fig.legend(handles[:-1], labels[:-1],ncol=len(labels),loc='lower left',bbox_to_anchor=(0.1,0.90),fontsize="large")
Area_file_prefix["file_prefix"]=file_prefix
fig.savefig("../results/Area_updated_fit__{}.png".format(file_prefix))
Area_replicas=pd.concat([Area_replicas,Area_file_prefix])